import Vue from "vue";
import App from "./App";
import Router from "vue-router";
import myRouter from "./router";
import "@assets/styles/varibless.less";
import "@assets/styles/reset.css";
import "@assets/styles/index.less";
import "@assets/icon/iconfont.css"
import "ant-design-vue/dist/antd.css";
import store from "@store/index.js";
import Antd from "ant-design-vue";
import { buttonPermissions } from "@/directive/permissions";
import { dragModal } from "@/directive/dragModal";
import formCreate from "@form-create/ant-design-vue";
const Plugins = [buttonPermissions, dragModal];

Plugins.map(plugin => {
  Vue.use(plugin);
});

Vue.use(Antd);
Vue.use(Router);
Vue.use(formCreate);

// 主题切换
const styleDefine = localStorage.getItem("GLOBAL_THEME"); //缓存的变量
let loadStyle;
async function importCss() {
  if (styleDefine === "dark") {
    loadStyle = await import("@assets/styles/theme/dark.less");
  } else {
    loadStyle = await import("@assets/styles/theme/light.less");
  }
  return loadStyle;
}
importCss();

// Vue.$message.config({
//   duration: 3, // 持续时间
//   top: `150px`, // 到页面顶部距离
//   maxCount: 3, // 最大显示数, 超过限制时，最早的消息会被自动关闭
// });

const on = Vue.prototype.$on;
// 防抖处理
Vue.prototype.$on = function(event, func) {
  let timer;
  let newFunc = func;
  if (event === "click") {
    newFunc = function() {
      clearTimeout(timer);
      timer = setTimeout(function() {
        func.apply(this, arguments);
      }, 500);
    };
  }
  on.call(this, event, newFunc);
};

Vue.prototype.$on = function(event, func) {
  let previous = 0;
  let newFunc = func;
  if (event === "click") {
    newFunc = function() {
      const now = new Date().getTime();
      if (previous + 1000 <= now) {
        func.apply(this, arguments);
        previous = now;
      }
    };
  }
  on.call(this, event, newFunc);
};

Vue.config.productionTip = false;

const vueInstance = new Vue({
  el: "#app",
  router: myRouter,
  store,
  components: { App },
  render: h => h(App)
}).$mount("#app");

export default vueInstance;
